const express=require('express');
const pool=require('../pool.js')
const router=express.Router();

// 获取用户信息
router.get('/userinfo',(req,res,next)=>{
  pool.query('select * from userinfo',(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.length){
        res.send({code:200,msg:'数据获取成功',data:result})
      }else{
        res.send({code:201,msg:'数据获取失败'})
      }
    }
  })
})

// 添加用户
router.get('/insertuser',(req,res,next)=>{
  let userinfo = JSON.parse(req.query.userinfo)
  userinfo.salary=Number(userinfo.salary)
  pool.query('insert into userinfo set ?',userinfo,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.affectedRows){
        res.send({code:200,msg:'添加成功'})
      }else{
        res.send({code:201,msg:'添加失败'})
      }
    }
  })
})

// 修改用户
router.get('/updata',(req,res,next)=>{
  let userinfo = JSON.parse(req.query.userinfo)
  userinfo.salary=Number(userinfo.salary)
  userinfo.id=Number(userinfo.id)
  let {id,name,salary,gander} = userinfo
  pool.query('update userinfo set name=?,salary=?,gander=? where id=?',[name,salary,gander,id],(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.affectedRows){
        res.send({code:200,msg:'修改成功'})
      }else{
        res.send({code:201,msg:'修改失败'})
      }
    }
  })
})

// 删除用户
router.get('/deluser',(req,res,next)=>{
  pool.query('delete from userinfo where id=?',req.query.id,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      if(result.affectedRows){
        res.send({code:200,msg:'删除成功'})
      }else{
        res.send({code:201,msg:'删除失败'})
      }
    }
  })
})
// 搜索用户
router.get('/search',(req,res,next)=>{
  // req.query.search=Number(req.query.search)
  // console.log(typeof(req.query.search))
  // console.log(typeof(req.query.search-5),req.query.search-5)
  // console.log(typeof(req.query.search-5)=='number' && !isNaN(req.query.search-5))
  if(req.query.search=='男' || req.query.search=='女'){
    let gander= req.query.search=='男'?'1':'0'
    pool.query('select * from userinfo where gander=?',gander,(err,result)=>{
      if(err){
        next(err)
        return
      }else{
        if(result.length){
          res.send({code:200,msg:'查询成功',data:result})
        }else{
          res.send({code:201,msg:'查询失败，无该用户',data:null})
        }
      }
    })
  }
  else if(typeof(req.query.search-5)=='number' && !isNaN(req.query.search-5)){
    let salary = Number(req.query.search)
    pool.query('select * from userinfo where salary=?',salary,(err,result)=>{
      if(err){
        next(err)
        return
      }else{
        if(result.length){
          res.send({code:200,msg:'查询成功',data:result})
        }else{
          res.send({code:201,msg:'查询失败，无该用户',data:null})
        }
      }
    })
  }else{
    let name = req.query.search
    pool.query(`select * from userinfo where name like "%${name}%"`,(err,result)=>{
      if(err){
        next(err)
        return
      }else{
        if(result.length){
          res.send({code:200,msg:'查询成功',data:result})
        }else{
          res.send({code:201,msg:'查询失败，无该用户',data:null})
        }
      }
    })
  }
})
module.exports=router;
